package Zoo.handler;


import Zoo.entity.UserEntity;
import Zoo.impl.UserServiceImpl;
import Zoo.service.UserService;
import Zoo.view.userview.UserView;
import Zoo.view.userview.UpdateUserView;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 用户修改个人信息界面处理类
 */
public class UpdateUserViewHandler implements ActionListener {
    private UpdateUserView updateUserView;
    private UserView userView;


    public UpdateUserViewHandler(UpdateUserView updateUserView, UserView userView) {
        this.updateUserView = updateUserView;
        this.userView = userView;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton jButton = (JButton) e.getSource();
        String text = jButton.getText();
        if ("修改".equals(text)) {
            UserService userService = new UserServiceImpl();
            UserEntity userEntity = updateUserView.buildUpdatedUserEntity();
            boolean updateResult = userService.update(userEntity);
            if (updateResult) {
                //重新加载表格查到最新数据
                userView.reloadTable();
                updateUserView.dispose();
            } else {
                JOptionPane.showMessageDialog(updateUserView, "修改失败");

            }

        }
    }

}
